<template>
	<view class="cu-modal " :class="showMyPoster?'show':''">
		<view class="cu-dialog my-prize-dialog poster">			
			<view id="poster_content" class="content">
				<view class="close" @tap="hide">
					<uni-icons type="closeempty" size="30" color="#fff"></uni-icons>
				</view>
				<image :src="posterUrl" style="height: 60vh;" mode="heightFix"></image>
				<view class="qrcode-content">
					<view style="width: 175px;height:175px;background-color: #fff;padding: 10rpx;">
						<canvas id="qrcode" canvas-id="qrcode" style="width: 175px;height:175px;" />
					</view>
					<view class="tips">长按识别二维码</view>
				</view>
			</view>
		</view>
		<view class="bottom">
			长按上方海报 “转发给好友”
		</view>
	</view>
</template>

<script>
	import UQRCode from 'uqrcodejs';
	import html2canvas from 'html2canvas'
	import { hb_class } from '../utils/index.js'
	export default {
		data(){
			return {
				showMyPoster: false,
				myPosterlist: [],
				posterUrl: ''
			}
		},
		methods: {
			// 获取海报链接地址
			getLink(openid){
				this.$comm.http('/api/activelove/share_url', {openid},"POST").then(res=>{
					if(res.code === 404){
						this.$emit('onInit')
						return
					}
					const qr = new UQRCode();
					qr.data = res.msg.url;
					qr.size = 175;
					qr.make();
					const ctx = uni.createCanvasContext('qrcode', this);
					qr.canvasContext = ctx;
					qr.drawCanvas();
					
					uni.canvasToTempFilePath({
						canvasId: 'qrcode',
						success: (res) => {
							var hbinfo={back:{src:"./static/images/poster.jpg",w:801,h:1315},er:{src:res.tempFilePath,w: 175,h:175,pos:{left: 313,top: 1095,}}};
							
							const qr = new hb_class(hbinfo)
							qr.init()
							qr.create().then(res=>{
								this.posterUrl = res
							})
						},
						fail: (err) => {
							console.error(err);
						}
					});		
				})
			},
			// 下载海报
			download(){
				html2canvas(document.getElementById("poster_content")).then(canvas=>{
					var domA = document.createElement("a");
					domA.download = '活动海报';
					domA.href = canvas.toDataURL("image/png");
					domA.click();
				})
			},
			open(openid){
				this.showMyPoster = true
				this.getLink(openid)
			},
			hide(){
				this.showMyPoster = false
			}
		}
	}
</script>

<style lang="scss">
	.poster{
		background-color: transparent;
	}
	.content{
		position: relative;
		.qrcode-content{
			background-color: #bc0c1b;
			width: 150rpx;
			height: 182rpx;
			position: absolute;
			bottom: 180rpx;
			right: -250rpx;
			.tips{
				font-size: 20rpx;
				color: #fff;
			}
		}
	}
	.bottom{
		position: fixed;
		bottom: 0;
		background-color: #fff;
		width: 100%;
		height: 100rpx;
		padding: 20rpx;
		line-height: 50rpx;
		
		display: flex;
		justify-content: space-around;
		color: #000;
		font-weight: bold;
		.cancel{
			width: 25%;
			border-radius: 35rpx;
			background: linear-gradient(0deg, #a1c4fd, #c2e9fb);
			padding: 10rpx;
			line-height: 40rpx;
		}
		.save{
			width: 25%;
			border-radius: 35rpx;
			padding: 10rpx;
			background: linear-gradient(0deg, #4facfe, #00f2fe);
			line-height: 40rpx;
		}
	}
</style>